package gunfire.relics;

import basemod.abstracts.CustomRelic;
import com.badlogic.gdx.graphics.Texture;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.CardLibrary;
import com.megacrit.cardcrawl.helpers.ModHelper;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rewards.RewardItem;
import gunfire.GunfireMod;
import gunfire.util.TextureLoader;

import java.util.ArrayList;
import java.util.Iterator;

import static gunfire.GunfireMod.*;

public class BreakTheIce extends CustomRelic {

    public static final String ID = GunfireMod.makeID("BreakTheIce");
    private static final Texture IMG = TextureLoader.getTexture(makeRelicPath("BreakTheIce.png"));
    private static final Texture OUTLINE = TextureLoader.getTexture(makeRelicOutlinePath("blank.png"));

    public BreakTheIce() {
        super(ID, IMG, OUTLINE, RelicTier.SHOP, LandingSound.CLINK);
    }

    @Override
    public void onEquip() {
        ArrayList<AbstractRelic> list = new ArrayList<>();
        for (AbstractRelic r : AbstractDungeon.player.relics){
            if(!r.relicId.contains("gunfire:SoulOf") && !r.relicId.equals("gunfire:BreakTheIce")){
                list.add(r);
            }
        }

        int loseIndex = AbstractDungeon.relicRng.random(list.size() - 1);
        String loseName = list.get(loseIndex).name;
        AbstractDungeon.player.loseRelic(list.get(loseIndex).relicId);

        AbstractDungeon.getCurrRoom().rewards.add(new MyRewardItem(AbstractCard.CardRarity.RARE));
        AbstractDungeon.getCurrRoom().rewards.add(new MyRewardItem(AbstractCard.CardRarity.UNCOMMON));
        AbstractDungeon.getCurrRoom().rewards.add(new MyRewardItem(AbstractCard.CardRarity.UNCOMMON));
        AbstractDungeon.getCurrRoom().rewards.add(new MyRewardItem(AbstractCard.CardRarity.COMMON));
        AbstractDungeon.getCurrRoom().rewards.add(new MyRewardItem(AbstractCard.CardRarity.COMMON));

        AbstractDungeon.combatRewardScreen.open(Settings.language == Settings.GameLanguage.ZHS
                ? "你失去了：" + loseName
                : "You lost: " + loseName);
        AbstractDungeon.getCurrRoom().rewardPopOutTimer = 0.0F;

        int remove = -1;

        for(int i = 0; i < AbstractDungeon.combatRewardScreen.rewards.size(); ++i) {
            if (AbstractDungeon.combatRewardScreen.rewards.get(i).type == RewardItem.RewardType.CARD
                    && AbstractDungeon.combatRewardScreen.rewards.get(i).goldAmt == 0) {
                remove = i;
                break;
            }
        }

        if (remove != -1) {
            AbstractDungeon.combatRewardScreen.rewards.remove(remove);
        }

        if(Settings.language == Settings.GameLanguage.ZHS){
            CardCrawlGame.sound.play("BreakTheIce");
        }
    }

    @Override
    public String getUpdatedDescription() {
        return DESCRIPTIONS[0];
    }

    @Override
    public AbstractRelic makeCopy() {
        return new BreakTheIce();
    }

    public static class MyRewardItem extends RewardItem {

        public MyRewardItem(AbstractCard.CardRarity rarity){
            super();
            this.goldAmt = 1; //用作标记
            this.cards = this.getRewardCards(rarity);
            if(rarity == AbstractCard.CardRarity.RARE){
                this.text = Settings.language == Settings.GameLanguage.ZHS
                        ? "将一张 #y稀有 牌加入你的牌组"
                        : "Add a #yRare card to your deck";
            } else if(rarity == AbstractCard.CardRarity.UNCOMMON){
                this.text = Settings.language == Settings.GameLanguage.ZHS
                        ? "将一张 #b罕见 牌加入你的牌组"
                        : "Add a #bUncommon card to your deck";
            } else {
                this.text = Settings.language == Settings.GameLanguage.ZHS
                        ? "将一张 [#999999]普通[] 牌加入你的牌组"
                        : "Add a [#999999]Common[] card to your deck";
            }
        }

        private ArrayList<AbstractCard> getRewardCards(AbstractCard.CardRarity rarity){
            AbstractPlayer player = AbstractDungeon.player;
            ArrayList<AbstractCard> retVal = new ArrayList<>();
            int numCards = 3;

            AbstractRelic r;
            for(Iterator var2 = player.relics.iterator(); var2.hasNext(); numCards = r.changeNumberOfCardsInReward(numCards)) {
                r = (AbstractRelic)var2.next();
            }

            if (ModHelper.isModEnabled("Binary")) {
                --numCards;
            }

            AbstractCard card;
            for(int i = 0; i < numCards; ++i) {
                card = null;

                boolean containsDupe = true;

                while(true) {
                    while(containsDupe) {
                        containsDupe = false;
                        if (player.hasRelic("PrismaticShard")) {
                            card = CardLibrary.getAnyColorCard(rarity);
                        } else {
                            card = AbstractDungeon.getCard(rarity);
                        }

                        for (AbstractCard c : retVal) {
                            if (c.cardID.equals(card.cardID)) {
                                containsDupe = true;
                                break;
                            }
                        }
                    }

                    if (card != null) {
                        retVal.add(card);
                    }
                    break;
                }
            }

            ArrayList<AbstractCard> retVal2 = new ArrayList<>();
            Iterator var11 = retVal.iterator();

            while(var11.hasNext()) {
                card = (AbstractCard)var11.next();
                retVal2.add(card.makeCopy());
            }

            var11 = retVal2.iterator();

            while(true) {
                while(var11.hasNext()) {
                    card = (AbstractCard)var11.next();
                    if (card.rarity != AbstractCard.CardRarity.RARE && AbstractDungeon.cardRng.randomBoolean(0F) && card.canUpgrade()) {
                        card.upgrade();
                    } else {
                        Iterator var12 = player.relics.iterator();

                        while(var12.hasNext()) {
                            AbstractRelic rr = (AbstractRelic)var12.next();
                            rr.onPreviewObtainCard(card);
                        }
                    }
                }

                return retVal2;
            }
        }
    }

}
